package com.ewing.busi.carry.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ewing.busi.carry.dao.CarryTemplateDao;
import com.ewing.busi.carry.dao.CarryWayDao;
import com.ewing.busi.carry.exception.CarryTemplateException;
import com.ewing.busi.carry.model.WebCarryTemplate;
import com.ewing.busi.carry.model.WebCarryWay;
import com.ewing.busi.system.model.SysParam;
import com.ewing.core.app.constant.IsEff;
import com.ewing.core.jdbc.BaseDao;

/**
 * 
 * @author tanson lam
 * @create Aug 27, 2016
 * 
 */
@Repository("carryTemplateService")
public class CarryTemplateService {
	@Resource
	private CarryTemplateDao carryTemplateDao;
	@Resource
	private CarryWayDao carryWayDao;
	@Resource
	private BaseDao baseDao;

	/**
	 * 保存运费模板
	 * 
	 * @param webCarryTemplate
	 * @param webCarryWays
	 * @throws CarryTemplateException
	 */
	@Transactional(rollbackFor = Exception.class)
	public WebCarryTemplate saveOrUpdateTemplate(
			WebCarryTemplate webCarryTemplate, List<WebCarryWay> webCarryWays)
			throws CarryTemplateException {
		webCarryTemplate.setIseff(IsEff.EFFECTIVE);
		if (webCarryTemplate.getId() == null) {
			WebCarryTemplate existTemplate = carryTemplateDao
					.findTemplateByName(webCarryTemplate.getName(),
							webCarryTemplate.getShopId());
			if (existTemplate != null)
				throw new CarryTemplateException("存在相同的运费模板名称");
			baseDao.save(webCarryTemplate);
		} else {
			baseDao.update(webCarryTemplate);
			carryWayDao.deleteByTemplateId(webCarryTemplate.getId());
		}

		for (WebCarryWay webCarryWay : webCarryWays) {
			webCarryWay.setCarryTemplateId(webCarryTemplate.getId());
			webCarryWay.setIseff(IsEff.EFFECTIVE);
			baseDao.save(webCarryWay);
		}
		return webCarryTemplate;
	}

	/**
	 * 获取运费模板的，并且转换成系统参数格式
	 * 
	 * @param shopId
	 * @return
	 */
	public List<SysParam> findTemplateForParamCode(Integer shopId) {
		List<WebCarryTemplate> tempList = carryTemplateDao
				.findTemplateByShopId(shopId);
		List<SysParam> sysParamList = new ArrayList<SysParam>();
		for (WebCarryTemplate g : tempList) {
			SysParam p = new SysParam();
			p.setParamValue(g.getId().toString());
			p.setParamName(g.getName());
			sysParamList.add(p);
		}
		return sysParamList;
	}

	/**
	 * 查找单个模板
	 * 
	 * @param templateId
	 * @param shopId
	 * @return
	 */
	public WebCarryTemplate findTemplate(Integer templateId, Integer shopId) {
		return carryTemplateDao.findTemplate(templateId, shopId);
	}

	/**
	 * 删除模板
	 * 
	 * @param templateId
	 * @param shopId
	 * @throws CarryTemplateException
	 */
	public void deleteTemplate(Integer templateId, Integer shopId)
			throws CarryTemplateException {
		WebCarryTemplate existTemplate = carryTemplateDao.findTemplate(
				templateId, shopId);
		if (existTemplate == null)
			throw new CarryTemplateException("刪除失败，没有对应的运费模板");
		baseDao.delete(existTemplate);
	}

}
